#include "CheckInSystem.h"
#include "Reservation.h"
#include "CheckInRecord.h"
#include "RoomAssigner.h"
#include <fstream>
#include <iostream>
#include <cctype>
#include <algorithm>

CheckInSystem::CheckInSystem(const std::string& rf, const std::string& cf)
: resFile(rf), checkInFile(cf) {}

void CheckInSystem::createDefaultReservations() const {
	std::ofstream out(resFile);
	out << "ORD001|张三|123456789012345678|大床房|2025-11-20|2025-11-22|0\n";
	out << "ORD002|李四|876543210987654321|双床房|2025-11-18|2025-11-21|0\n";
	out.close();
	std::cout << "已自动生成测试预订文件: " << resFile << "\n";
}

std::vector<Reservation> CheckInSystem::loadReservations() const {
	std::ifstream file(resFile);
	if (!file.is_open()) {
		createDefaultReservations();
		file.open(resFile);
	}
	
	std::vector<Reservation> list;
	std::string line;
	while (std::getline(file, line)) {
		if (!line.empty()) {
			list.push_back(Reservation::fromString(line));
		}
	}
	file.close();
	return list;
}

void CheckInSystem::saveReservations(const std::vector<Reservation>& list) const {
	std::ofstream file(resFile);
	for (const auto& r : list) {
		file << r.toString() << "\n";
	}
}

std::vector<CheckInRecord> CheckInSystem::loadCheckIns() const {
	std::vector<CheckInRecord> list;
	std::ifstream file(checkInFile);
	if (!file.is_open()) return list;
	
	std::string line;
	while (std::getline(file, line)) {
		if (!line.empty()) {
			list.push_back(CheckInRecord::fromString(line));
		}
	}
	file.close();
	return list;
}

void CheckInSystem::saveCheckIns(const std::vector<CheckInRecord>& list) const {
	std::ofstream file(checkInFile);
	for (const auto& r : list) {
		file << r.toString() << "\n";
	}
}

bool CheckInSystem::processCheckIn() {
	auto reservations = loadReservations();
	auto checkIns = loadCheckIns();
	
	std::cout << "=== MR酒店 入住登记系统 ===\n";
	std::string idCard;
	std::cout << "请输入18位身份证号码：";
	std::cin >> idCard;
	
	if (idCard.length() != 18 || !std::all_of(idCard.begin(), idCard.end(), ::isdigit)) {
		std::cerr << "身份证必须为18位纯数字！\n";
		return false;
	}
	
	Reservation* target = nullptr;
	for (auto& r : reservations) {
		if (r.idCard == idCard && !r.isCheckedIn) {
			target = &r;
			break;
		}
	}
	
	if (!target) {
		std::cout << "未找到匹配的未入住预订！\n";
		std::cout << "当前可入住的订单：\n";
		for (const auto& r : reservations) {
			if (!r.isCheckedIn) {
				std::cout << "- " << r.guestName << " (" << r.idCard << ") - " << r.roomType << "\n";
			}
		}
		return false;
	}
	
	float deposit;
	std::cout << "找到预订：姓名=" << target->guestName << "，房型=" << target->roomType << "\n";
	std::cout << "请输入押金金额（元）：";
	if (!(std::cin >> deposit) || deposit <= 0) {
		std::cerr << "押金必须是大于0的数字！\n";
		return false;
	}
	
	int roomNum = RoomAssigner::assignRoomNumber(target->roomType);
	
	CheckInRecord rec;
	rec.orderId = target->orderId;
	rec.idCard = target->idCard;
	rec.guestName = target->guestName;
	rec.roomType = target->roomType;
	rec.roomNumber = roomNum;
	rec.actualCheckInTime = getCurrentTime();
	rec.deposit = deposit;
	rec.isCheckedOut = false;
	
	target->isCheckedIn = true;
	saveReservations(reservations);
	checkIns.push_back(rec);
	saveCheckIns(checkIns);
	
	rec.printSlip();
	std::cout << "入住成功！已分配房号 " << roomNum << "，数据已保存至硬盘。\n";
	return true;
}
